#-*- coding: utf-8 -*-
# @Time    : 2018-05-15 15:19
# @Author  : Yang Feng
# @Site    : 
# @File    : test.py
# @Software: PyCharm

import json
import codecs
import sys
import os
reload(sys)
sys.setdefaultencoding('utf-8')

ratingRequest = json.loads(u"{\"ratingRequest\":{\"applyNumber\":\"OI2018050500001\",\"companyName\":\"测试IG公司\",\"creditCode\":\"915001187428619429\",\"regNumber\":\"\",\"legalPerson\":\"魏永香\",\"legalPersonIdCard\":\"510123199309272313\",\"sceneCode\":\"YSD\",\"amount\":730000,\"timeLimit\":20,\"cellPhoneNum\":\"18211112222\",\"repaymentWay\":\"先息后本\",\"accessToken\":\"KINBxxxxxxxxxx\",\"yancaozhengStartDate\":\"2015-03-05\",\"yancaozhengEndDate\":\"2018-09-09\",\"yanshangAuthId\":\"xxxx\",\"yanshangAccount\":null,\"yanshangPassword\":null,\"personBankBusiTypeArr\":\"\",\"compBankBusiTypeArr\":\"\",\"notAllowIndustry\":\"[{\\\"industryNo\\\":\\\"F\\\",\\\"industryName\\\":\\\"xxxx\\\"},{\\\"industryNo\\\":\\\"H\\\",\\\"industryName\\\":\\\"xxxx\\\"}]\",\"icr\":{},\"ecr\":{}}}")
# 黑名单 身份证号[11111, 22222] 统一社会信用编码[3333, 4444]
BlackList = json.loads(u"{\"BlackList\":{\"code\":\"200\",\"msg\":\"成功\",\"data\":[{\"id\":1,\"blackCustomerName\":null,\"blackCertype\":\"Ent01\",\"blackCertyno\":\"11111\"},{\"id\":1,\"blackCustomerName\":null,\"blackCertype\":\"Ent01\",\"blackCertyno\":\"22222\"},{\"id\":1,\"blackCustomerName\":null,\"blackCertype\":\"Ind01\",\"blackCertyno\":\"3333\"},{\"id\":1,\"blackCustomerName\":null,\"blackCertype\":\"Ind01\",\"blackCertyno\":\"4444\"}]}}")


return_list = []
with open("E:\\CQ\\Workspace\\changjiang_cigarette\\Test\\Input\\"
          "ic_test.json") as f:
    result = {"success": True}
    data_list = f.readlines()
    for data in data_list:
        bs = data.split("@@")[0]
        json_data = data.split("@@")[1]
        context = json.loads(json_data)
        context.update(ratingRequest)
        context.update(BlackList)
        execfile("E:\\CQ\\Workspace\\changjiang_cigarette\\Index\\"
                 "IG\\Gender.py", context)
        result["result"] = unicode(context["result"])
        return_list.append("%s@@%s" % (bs, json.dumps(result, ensure_ascii=False, encoding='utf-8')))

for i in return_list:
    print i

f = codecs.open('E:/changjiang_cigarette/Test/chenbin/result/TinyCompanyLoanCnt.txt', 'w', 'utf-8')
for return_data in return_list:
    f.write(return_data)
    f.write("\n")
f.close()


def run(json_path, path, index, pyfile):
    return_list = []
    with open(json_path) as f :
        result = {"success": True}
        data_list = f.readlines()
        for data in data_list:
            bs = data.split("@@")[0]
            json_data = data.split("@@")[1]
            context = json.loads(json_data)
            ex_file = os.path.join(path,index)
            ex_file = os.path.join(ex_file, pyfile)
            execfile(ex_file, context)
            result["result"] = unicode(context["result"])
            result['code'] = unicode(pyfile)
            return_list.append("%s@@%s" % (bs, json.dumps(result, ensure_ascii=False, encoding='utf-8')))
    for i in return_list:
        print i

json_path = "E:\\CQ\\Workspace\\changjiang_cigarette\\Test\\Input\\ic_test.json"
json_path = "E:\\CQ\\Workspace\\changjiang_cigarette\\Test\\Input\\EG\\yangli.json"

path = "E:\\CQ\\Workspace\\changjiang_cigarette\\Index"
index = 'Rule'
for file in os.listdir(os.path.join(path, index)):
    if file.startswith('U'):
        run(json_path, path, index, pyfile=file)